import utils from '../../../utils/util'
const app = getApp();
const { request, server } = app;
Component({
  options: {
    addGlobalClass: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    data:{
      type: Object,
      value: null
    },
    navMode:{
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    aniExecCount:0,
    allAniEnd: false 
  },

  lifetimes:{
    attached() {
      this.initList()
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    itemVisible(e) {
      const { item, index, childIndex } = e.currentTarget.dataset;
      this.setData({
        [`data[${index}].data[${childIndex}].img_url`]: item.original_img
      })
    },
    async initList() {
      let res = this.data.data;
      if(!res) {
        const data = await this.rankTopInterface();
        res = data.result;
      }
      this.setData({
        data: res
      }, () => {
        this.triggerEvent('rankTopRenderComplete', res)
      });
      this.triggerEvent('setData', this.data.data)
    },
    rankTopInterface(){
      return new Promise((resolve,reject) => {
        server.request(`${app.globalData.bojem_siteUrl}/api/top_sale_rank/index`, 'GET', {
          onSuccess(res) {
            res = res.data
            resolve(res)
          },
          onFail(e) {
            reject(e)
          }
        })
      })
    },
    toDetailPage(e) {
      if(this.data.navMode){
        return this.triggerEvent('navEvent', e)
      }
      utils.jumpToPage(`/subpackage/shop/goodsInfo/goodsInfo?goods_id=${item.goods_id}&sid=${item.activity_id}&platform=${this.data.platform}`)
    },
    lazyAniEnd(){
      this.setData({
        aniExecCount: this.data.aniExecCount+1
      })
      if(this.data.aniExecCount == this.data.data.length){
        this.setData({
          allAniEnd: true
        })
      }
    }
  }
})
